package model;

import java.awt.geom.Point2D;
import java.util.ArrayList;

public class Secant {
	public static ArrayList<Iteration> approximate(Polynomial poly, Point2D.Double point1, Point2D.Double point2, int maxIterations, double errorThreshold){
		ArrayList<Iteration> iterations = new ArrayList<Iteration>();
		
		double currThreshold = errorThreshold + 1;
		
		for(int i = 0;i < maxIterations && currThreshold > errorThreshold; i++){
			double slope = (point2.y- point1.y) / (point2.x - point1.x);
			double newX = point2.x - point2.y/slope;
			
			Point2D.Double newPoint = new Point2D.Double(newX, poly.evaluate(newX));
			
			iterations.add(new Iteration(point1, point2, newPoint));
			
			point1 = point2;
			point2 = newPoint;
			
			currThreshold = Math.abs(point1.x - point2.x);
		}
	
		return iterations;
	}
}
